﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


/**
 * 委托应用：模板方法
 */
namespace P19_3 {
    internal class Program {
        static void Main(string[] args) {

            ProductFactory productFactory = new ProductFactory();
            WrapFactory wrapFactory = new WrapFactory();
            //委托
            Func<Product> fun1 = new Func<Product>(productFactory.MakePizza);
            Func<Product> fun2 = new Func<Product>(productFactory.MakeToyCar);

            Box box1 = wrapFactory.WrapProduct(fun1);
            Box box2 = wrapFactory.WrapProduct(fun2);
            Console.WriteLine(box1.Prod.Name);
            Console.WriteLine(box2.Prod.Name);
            Console.ReadLine();
        }
    }

    class Product { 
        public string Name { get; set; }
    }
    class Box { 
        public Product Prod { get; set; }
    }
    class WrapFactory {
        public Box WrapProduct(Func<Product> getProduct) {
            Box box = new Box();
            Product product = getProduct.Invoke();
            box.Prod = product;
            return box;
        }
    }

    class ProductFactory {
        public Product MakePizza() {
            Product product = new Product();
            product.Name = "Pizza";
            return product;
        }

        public Product MakeToyCar() { 
            Product prod = new Product();
            prod.Name = "Toy Car";
            return prod;
        }
    }
}
